package com.googlecode.dgwt.rebind;

import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.NotFoundException;
import com.google.gwt.core.ext.typeinfo.TypeOracle;

/**
 * @author      Wellington Tadeu dos Santos
 * @since       DGWT v0.01
 */
public class ReflectionGenerator extends Generator {
    
    @Override
    public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
        
        TypeOracle typeOracle = context.getTypeOracle();
        JClassType targetClass;

        try {
            targetClass = typeOracle.getType(typeName);
        } catch (NotFoundException e) {
            logger.log(TreeLogger.ERROR, "No such type", e);
            throw new UnableToCompleteException();
        }
        
        return new ReflectionSourceCreator(context, logger, typeOracle).emitClass(targetClass);       
    }

}
